<template>
	<div class="easyd-weixin-Box ">
		<div class="app_page_box" style="bottom:50px;">
			<div class="app_page">

				<div class="app_list_box MARGIN_BOTTOM_10">
					<div class="app_box_content">
						<div class="app_box_content_left">
							微信昵称
						</div>
						<div class="app_box_content_right" @click="weChatCustomer()">
							<span>{{form.wx_nickname || "请选择"}}</span>
							<!--<span>{{$route.query.wx_nickname || "请选择"}}</span>-->
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							姓名
						</div>
						<div class="app_box_content_right">
							<input class="T1 C4" type="text" name="" id="" v-model="form.name" placeholder="请输入" value="" />
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							电话
						</div>
						<div class="app_box_content_right">
							<input class="T1 C4" type="text" name="" id="" v-model="form.tel" placeholder="请输入" value="" />
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							校区名称
						</div>
						<div class="app_box_content_right">
							<input class="T1 C4" type="text" name="" id="" v-model="form.schoolName" placeholder="请输入" value="" />
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							校区面积
						</div>
						<div class="app_box_content_right C4">
							<input class="T1 C4" type="text" name="" id="" v-model="form.schoolArea" placeholder="请输入" value="" />
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							校区学员数
						</div>
						<div class="app_box_content_right C4">
							<input class="T1 C4" type="text" name="" id="" v-model="form.schoolPeople" placeholder="请输入" value="" />
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							校区地址
						</div>
						<div class="app_box_content_right C4" @click="address()">
							<span v-if="shengData.l_name">{{shengData.l_name + shiData.l_name + quData.l_name + addrData}}</span>
							<span v-else>请选择</span>
							
							<!--{{txtData}}-->
							<!--{{form.schoolLocation}}-->
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							在校职位
						</div>
						<div class="app_box_content_right">
							<input class="T1 C4" type="text" name="" id="" v-model="form.schoolPost" placeholder="请输入" value="" />
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							开办时间
						</div>
						<div class="app_box_content_right" @click="selectStartTime()">
							<span>{{form.campusStartTime}}</span>
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							教授类型
						</div>
						<div class="app_box_content_right C4" @click="artCategory()">
							<span v-if="!form.schoolTypeStr">请选择</span>
							<span v-else>{{form.schoolTypeStr}}</span>
						</div>
					</div>
				</div>

				<div class="app_list_box MARGIN_BOTTOM_10">
					<div class="app_box_content">
						<div class="app_box_content_left">
							是否为KP
						</div>
						<div class="app_box_content_right C4" @click="isKP()">
							<!--{{form.isKP.name}}-->
							<span v-if="form.isKP.id == '1'">是</span>
							<span v-else>否</span>
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							客户评定
						</div>
						<div class="app_box_content_right C4" @click="evaluate()">
							<!--{{form.evaluate.name}}-->
							<span v-if="form.evaluate.id == '0'">一类客户</span>
							<span v-else-if="form.evaluate.id == '1'">二类客户</span>
							<span v-else-if="form.evaluate.id == '2'">三类客户</span>
							<span v-else-if="form.evaluate.id == '3'">四类客户</span>
							<span v-else-if="form.evaluate.id == '4'">五类客户</span>
							<span v-else>请选择</span>
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_left">
							校区现有系统
						</div>
						<div class="app_box_content_right" @click="selectSystem()">
							<span>{{form.campusNowSystem}}</span>
						</div>
					</div>
				</div>

				<div class="app_list_box MARGIN_BOTTOM_10">
					<div class="app_box_content">
						<div class="app_box_content_left">
							备注
						</div>
					</div>
					<div class="app_box_content">
						<div class="app_box_content_textarea">
							<textarea name="" placeholder="请填写其他信息" v-model="form.remark" rows="5" cols=""></textarea>
						</div>
					</div>
				</div>

			</div>
		</div>
		<div class="app_bottom_box">
			<div class="app_bottom_box_right" @click="save()">保存</div>
		</div>

	</div>
</template>

<script>
	import { mapState, mapMutations } from "vuex";
	export default {

		beforeRouteEnter(to, from, next) {
			if(from.path == '/address' ||
				from.path == '/weChatCustomer' ||
				from.path == '/schoolType' ||
				from.path == '/artCategory' ||
				from.path == '/isKP' ||
				from.path == '/evaluate') {
				to.meta.isBack = true;
			}
			next();
		},
		data() {
			return {
				
			};
		},
		computed: {
			...mapState({
				    form : state => state.AddClientModule.form,
					shengData: state => state.LocationModule.sheng,
					shiData: state => state.LocationModule.shi,
					quData: state => state.LocationModule.qu,
					addrData: state => state.LocationModule.addr,
					txtData: state => state.LocationModule.txt,
					locationId: state => state.LocationModule.locationId,
			})
		},
		mounted() {
			console.log(this.form)

			//如果为后退回来的界面,不在进行编辑数据的请求
			if(!this.$route.meta.isBack) {	
				if(this.$route.query.customerId){
					this.initData();
					console.log('正常打开带参数页面')
				}		
				console.log('正常打开页面')
			}else{
				console.log('返回页面')
			}
			
			if(this.$route.query.wx_nickname){
				this.form.wx_nickname = this.$route.query.wx_nickname;
				this.form.openid = this.$route.query.openid;
			}
			
			
			
			this.$route.meta.isBack = false;
		},
		methods: {

			//  ...mapMutations(["ShopIndexModule/updateIsShopGuide"]),

            initData() {
                var that = this;
                this.$easyUtil.startLoading(this);
                this.$http.getHttp(
                    this.$API.searchCustomer + "/c_id/" + this.$route.query.customerId,
                    rs => {
                        this.$easyUtil.endLoading(this);
                        console.log(rs);
                        var sheng = {
                        	l_id:rs.data[0].c_provinces,
                        	l_name:rs.data[0].c_provinces_name
                        }
                        var shi = {
                        	l_id:rs.data[0].c_city,
                        	l_name:rs.data[0].c_city_name
                        }
                        var qu = {
                        	l_id:rs.data[0].c_area,
                        	l_name:rs.data[0].c_area_name
                        }
                        that.$store.commit("AddClientModule/updateClientData", rs.data[0]);
                        that.$store.commit("LocationModule/updateShengData", sheng);
                        that.$store.commit("LocationModule/updateShiData", shi);
                        that.$store.commit("LocationModule/updateQuData", qu);
                        that.$store.commit("LocationModule/updateAddrData", rs.data[0].c_address);
                        that.$store.commit("LocationModule/updateLocationId", rs.data[0].c_pca_code);
                        
						that.$store.commit('AddClientModule/updateSchoolType',rs.data[0].c_teach_type);
						that.$store.commit('AddClientModule/updateSchoolTypeStr',rs.data[0].c_teach_type_text);

                    }
                );
            },
			artCategory(e) {
				this.$easyUtil.GO(this,'/artCategory');
			},
			schoolType(e) {
				this.$easyUtil.GO(this,'/schoolType');
			},

			isKP(e) {
				this.$easyUtil.GO(this,'/isKP');
			},

			evaluate(e) {
				this.$easyUtil.GO(this,'/evaluate');
			},

			address(e) {
				this.$easyUtil.GO(this,'/address');
			},

			weChatCustomer(e) {
				if(!this.$route.query.wx_nickname){
					this.$easyUtil.GO(this,'/weChatCustomer');
				}
			},

			save(e) {
	            if(this.$common.vEmpty(this.form.name)){
	                this.$easyUtil.errorToast(this,'姓名不能为空');  
	                return; 
	            }
	            if(!this.$common.vPhone(this.form.tel)){
	                this.$easyUtil.errorToast(this,'电话格式不正确');  
	                return; 
	            }
	            if(this.$common.vEmpty(this.form.schoolName)){
	                this.$easyUtil.errorToast(this,'校区名称不能为空');  
	                return; 
	            }
				console.log(this.form);
				
				var params = {
	                c_weixin_nickname : this.form.wx_nickname == undefined ? '' : this.form.wx_nickname ,//微信昵称
	                c_openid : this.form.openid == undefined ? '' : this.form.openid ,//openid
	                c_name  : this.form.name,
	                c_phone : this.form.tel,
	                c_provinces : this.shengData.l_id,//省
	                c_provinces_name : this.shengData.l_name,//省
	                c_city : this.shiData.l_id,//市
	                c_city_name : this.shiData.l_name,//市
	                c_area : this.quData.l_id,//区
	                c_area_name : this.quData.l_name,//区
	                c_address : this.addrData,//详细地址
	                c_uid : this.$userData.getUID(),//对接人ID（销售）
	                c_evaluation : this.form.evaluate.id ,//客户评定 0-A 1-B 2-C
	                c_area_of : this.form.schoolArea ,//场地面积（单位：平方米）
	                c_campus_name : this.form.schoolName ,//校区名称
	                c_student_count : this.form.schoolPeople ,//校区人数
	                c_teach_type : this.form.schoolType ,//教授类型
	                c_teach_type_text : this.form.schoolTypeStr ,//教授类型字符串
	                c_is_kp : this.form.isKP.id ,//是否为KP 0-否 1-是
	                c_note : this.form.remark ,//备注信息
	                c_position : this.form.schoolPost ,//在校职位
	                c_osid    : this.$userData.getOsid(),//所属节点id
	                c_pca_code    : JSON.stringify(this.locationId),//省市区序号
					c_campus_start_time: new Date(this.form.campusStartTime) / 1000,//校区开办时间
					c_campus_now_system: this.form.campusNowSystem,//现有校区系统
	            };
				/*console.log(params);
				return;*/
				var newUrls = this.$API.addCustomer;
				if(this.$route.query.customerId){
					newUrls = this.$API.updateCustomer;
					params.update_where = 'c_id';
					params.c_id = this.$route.query.customerId;
				}else{
					params.c_create_time = (new Date()/1000) ;//新增时间
	                params.u_leader    = this.$userData.getLeader();//上级ID
	                params.sell_name    = this.$userData.getNickname();//销售名
				}
				
				console.log(params)
				
		        this.$easyUtil.startLoading(this)
				this.$http.postHttp(newUrls,params,(rs)=>{
				
					this.$easyUtil.endLoading(this)
					
					if(this.$route.query.customerId){
						this.$easyUtil.successToast(this, '客户编辑成功');
					}else{
						this.$easyUtil.successToast(this, '客户录入成功');
					}
					this.$store.commit('AddClientModule/clearClientData');
					history.go(-1);
		
				})
			},
			selectStartTime(){
				this.$easyUtil.GO(this, '/followTime',{source:'addClient'})
			},
			selectSystem(){
				this.$easyUtil.GO(this, '/selectSystem',)
			}
		}
	};
</script>

<style lang="stylus" scoped>
	@import '~PUBLIC_CSS';
</style>